<?php
/**
 * Created by PhpStorm.
 * User: J.H
 * Date: 2018/7/8
 * Time: 14:13
 */

namespace lib\aliYun\market;


use lib\curl\HttpRequest;

class BaseApi
{
    const GET = 'GET';
    const POST = 'POST';
    protected $api_url = '';
    protected $params = array();
    protected $AppKey;
    protected $AppSecret;
    protected $AppCode;

    public function __construct(array $configs)
    {
        foreach ($configs as $key => $config) {
            $this->{$key} = $config;
        }
    }

    protected function exec($method = self::GET, &$http_code = 200, &$e_code = '', &$e_msg = '')
    {
        $curl = new HttpRequest();
        $curl->setHeader([
            "Content-Type: application/json; charset=UTF-8",
            "Authorization:APPCODE {$this->AppCode}"
        ]);
        return $curl->request($this->api_url, $this->params, $method, $e_code, $e_msg);
    }

    protected function parseJson($data)
    {
        $d = json_decode($data,true);
        if (JSON_ERROR_NONE !== json_last_error()) {
            return [];
        }
        return $d;
    }

}